package cn.edu.njtc.service.impl;

import cn.edu.njtc.common.ErrorMessage;
import cn.edu.njtc.entity.dto.UserDto;
import cn.edu.njtc.entity.dto.UserTokenDto;
import cn.edu.njtc.entity.pojo.User;
import cn.edu.njtc.exception.BizException;
import cn.edu.njtc.mapper.UserMapper;
import cn.edu.njtc.service.UserService;
import cn.edu.njtc.util.JWTUtil;
import cn.edu.njtc.util.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public User selectById(Integer id) throws BizException {
        User user = userMapper.selectById(id);
        if (user != null) {
            return user;
        }
        throw new BizException(ErrorMessage.USER_NOT_FOUND);
    }

    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public int add(User user) {
        if (user.getPassword() != null && user.getPassword().length() > 0) {
            user.setPassword(MD5Utils.md5Upper(user.getPassword()));
        }
        return userMapper.insert(user);
    }

    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int modify(User user) {
        if (user.getPassword() != null && user.getPassword().length() > 0) {
            user.setPassword(MD5Utils.md5Upper(user.getPassword()));
        }
        return userMapper.update(user);
    }

    @Override
    public List<User> selectLikeName(String name) {
        return userMapper.selectLikeName(name);
    }

    @Override
    public UserTokenDto login(UserDto userDto) throws BizException {
        userDto.setPassword(MD5Utils.md5Upper(userDto.getPassword()));
        //执行持久层方法返回对象，非空表示登录成功，否则抛出业务异常
        UserTokenDto userTokenDto = userMapper.selectByPasswordAndPhone(userDto);
        if (userTokenDto != null) {
            userTokenDto.setToken(JWTUtil.createJWT(userTokenDto.getPhone()));
            return userTokenDto;
        }
        throw new BizException(ErrorMessage.LOGIN_FAILED); // 魔法值，用枚举方式解决

    }

    public int register(UserDto userDto) throws BizException {
        int i = 0;
        if (userDto.getPhone() == null || userDto.getPhone().length() != 11) {
            throw new BizException(ErrorMessage.INVALID_PHONE_LENGTH);
        }
        User user = userMapper.selectByPhone(userDto.getPhone());
        if (user != null) {
            throw new BizException(ErrorMessage.DUPLICATE_PHONE);
        }
        else {
            if (userDto.getPassword() != null && userDto.getPassword().length() > 0) {
                userDto.setPassword(MD5Utils.md5Upper(userDto.getPassword()));
            }
            user = new User();
            BeanUtils.copyProperties(userDto, user);
            i = userMapper.insert(user);
        }
        return i;
    }

}
